/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package main;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.game.GameCanvas;

/**
 *
 * @author WIN7
 */
public class HealPokemonScreen extends GameCanvas implements Runnable, CommandListener{
    private MainMidlet midlet;
    private Item item;
    private Character character;
    private Command cmdBack = new Command("Back", Command.BACK, 0);
    private Command cmdHeal = new Command("Heal", Command.OK, 1);
    private boolean running = true;
    private boolean healing = false;
    public HealPokemonScreen(MainMidlet midlet, Item item, Character character)
    {
        super(true);
        setFullScreenMode(true);
        this.midlet = midlet;
        this.item = item;
        this.character = character;
        this.addCommand(cmdBack);
        this.addCommand(cmdHeal);
        this.setCommandListener(this);
    }
    public void stop()
    {
        running = false;
    }
    public void run() {
        Graphics g = getGraphics();
        while(running)
        {
            updateState();
            paint(g);
            try
            {
                Thread.sleep(150);
            }
            catch(Exception ex)
            {
                ex.printStackTrace();
                return;
            }
        }
    }
    public void updateState()
    {
        character.getOwnPokemon().getSprite().nextFrame();
        if(healing)
        {
            if(character.getOwnPokemon().healing(10))
            {
                stop();
                midlet.showStoryModeMenuOption(item, character);
            }
               
        }
    }
    public void start()
    {
        Thread t = new Thread(this);
        t.start();
    }
    public void paint(Graphics g)
    {
        g.setColor(0, 0, 255);
        g.fillRect(0, 0, getWidth(), getHeight());
        Font font = Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_BOLD, Font.SIZE_LARGE);
        g.setFont(font);
        g.setColor(255, 255, 0);
        String nameItem = item.getName();
        //draw Pokemon
        character.getOwnPokemon().paint(g, getWidth()/5, getHeight()/6, true, false);
        //draw Item name
        g.drawString(nameItem, getWidth()/2, 20, Graphics.BASELINE|Graphics.HCENTER);
        g.setColor(255, 0, 0);
        character.getOwnPokemon().drawInfo(g, getWidth()/5, getHeight()/6 + character.getOwnPokemon().getSprite().getHeight() + 10);
        //draw guide
        font = Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_PLAIN, Font.SIZE_MEDIUM);
        g.setFont(font);
        if(!healing)
        {
            g.drawString("Would you like to heal\n your pokemon?", 0, 
            getHeight()/2 + 50, Graphics.BASELINE|Graphics.LEFT);
            g.drawString("No", 0, getHeight() - 10, Graphics.BASELINE|Graphics.LEFT);
            g.drawString("Yes", getWidth(), getHeight() - 10, Graphics.BASELINE|Graphics.RIGHT);
        }
        
        flushGraphics();
    }

    public void commandAction(Command c, Displayable d) {
        if(c == cmdBack && !healing)
        {
            stop();
            midlet.showStoryModeMenuOption(item, character);
        }else if(c == cmdHeal && !healing)
        {
            healing = true;
        }
    }
    
}
